package com.wuxianedu.http;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    public static final String URL = "http://dldir1.qq.com/weixin/android/weixin6325android861.apk";

    ProgressDialog pd;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {

            pd.show();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        pd = new ProgressDialog(this);

        pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        new Thread() {
            @Override
            public void run() {
                try {


                    handler.sendEmptyMessage(0);

                    Download.down(URL, MainActivity.this, pd);

                    installApk(Download.path);

                    pd.dismiss();

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }.start();
    }


    //安装apk文件
    public void installApk(String path) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = Uri.parse("file://" + path);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        startActivity(intent);
    }
}
